<!--
 * @Descripttion: 银行专户流水明细
 * @version: 
 * @Author: zhangfan
 * @email: 2207044692@qq.com
 * @Date: 2020-12-28 11:19:48
 * @LastEditors: younghxp
 * @LastEditTime: 2022-02-15 11:05:48
-->
<template>
  <div class="container" ref="mainCon">
    <div class="mainCon">
      <el-row
        class="searchBox searchInputWidth"
        style="padding-bottom: 0; position: relative"
      >
        <el-form :inline="true" :model="filterField">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <!-- <el-input v-model="filterField.projectName" placeholder="请输入项目名称" clearable></el-input> -->
              <el-select
                v-model="filterField.projectName"
                filterable
                clearable
                remote
                reserve-keyword
                placeholder="请输入项目名称"
                :remote-method="remoteMethod"
                :loading="loading"
                @clear="clearOption"
              >
                <el-option
                  v-for="(item, i) in projectOptions"
                  :key="i"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="行业" class="row-padding-bottom">
                <el-select
                  v-model="filterField.industryId"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in industryOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="投资类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.investment"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in investmentOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                :label="isYcUser() || isYiLingUser() ? '监察员' : '网格管理员'"
                class="row-padding-bottom"
              >
                <el-select
                  v-model="filterField.userId"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in administratorArr"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="8">
            <el-form-item>
              <!-- <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchList" class="commonBtn"></el-button> -->
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <!-- 表格 -->
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <!-- 查询表格 -->
          <!-- <exportBtn v-if="isCzUser()" :btnText='`导出预警明细`' :responseType='`get`' :dataLen="tableList.length" :filterField="{...filterField,pageNo,pageSize}" :api="`api-s/report/export_JC_WG_CS_ALERT`"></exportBtn> -->
          <template v-if="isCzUser()">
            <el-popover placement="bottom" width="300" trigger="click">
              <template>
                <el-checkbox
                  :indeterminate="isIndeterminate"
                  v-model="checkAll"
                  @change="handleCheckAllChange"
                  style="margin-bottom: 12px"
                  >全选</el-checkbox
                >
                <el-checkbox-group
                  v-model="checkedCities"
                  @change="handleCheckedCitiesChange"
                >
                  <el-checkbox
                    v-for="item in cities"
                    :label="item.code"
                    :key="item.code"
                    >{{ item.name }}</el-checkbox
                  >
                </el-checkbox-group>
                <div style="text-align: right; margin-top: 10px">
                  <el-button type="primary" size="mini" @click="exportExcel"
                    >确定</el-button
                  >
                </div>
              </template>
              <el-button
                class="commonBut saveBtn funComBut"
                icon="icon iconfont icondaochu"
                slot="reference"
                style="margin-right: 10px"
                ><span class="opetareText">导出预警明细</span></el-button
              >
            </el-popover>
          </template>
          <el-button
            class="commonBtn saveBtn funComBut"
            style="vertical-align: top"
            @click="bnatchDistribution"
            ><span class="opetareText">批量分配</span>
          </el-button>
        </div>
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          @selection-change="handleSelectionChange"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <!-- <el-table-column prop="projectName" label="项目名称" width="200px"></el-table-column> -->
          <el-table-column label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="areaName"
            label="区域"
            align="center"
            min-width="120px"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="industryName"
            label="行业"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="investmentDesc"
            label="投资类型"
            align="center"
            :formatter="formatTd"
            width="120px"
          ></el-table-column>
          <el-table-column
            v-if="isCzUser()"
            prop="handle"
            label="未处理/处理中/已处理预警"
            align="center"
            :formatter="formatTd"
            width="220px"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                style="margin: 0 10px"
                @click="linkToNotice(scope.row, 1)"
                >{{ scope.row.handle || '0' }}</el-link
              >/<el-link
                type="primary"
                style="margin: 0 10px"
                @click="linkToNotice(scope.row, 2)"
                >{{ scope.row.handling || '0' }}</el-link
              >/<el-link
                type="primary"
                style="margin: 0 10px"
                @click="linkToNotice(scope.row, 3)"
                >{{ scope.row.handled || '0' }}</el-link
              >
            </template>
          </el-table-column>
          <el-table-column
            v-if="isCzUser()"
            prop="monthHandle"
            label="本月处理预警"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            width="140px"
          >
            <template slot-scope="scope">
              <el-link type="primary" @click="linkToNoticeMonth(scope.row)">{{
                scope.row.monthHandle || '0'
              }}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            v-if="!isCzUser()"
            sortable="custom"
            prop="alertTotal"
            label="预警"
            width="100"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            label="状态"
            align="center"
            :formatter="formatTd"
            width="120px"
          ></el-table-column>
          <!-- <el-table-column prop="gridName" label="网格名称" align="center" :formatter=formatTd></el-table-column> -->
          <el-table-column
            :label="isYcUser() || isYiLingUser() ? '乡镇名称' : '网格名称'"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.gridName"
                :columnName="'gridName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="nickName"
            :label="isYcUser() || isYiLingUser() ? '监察员' : '网格管理员'"
            align="center"
            :formatter="formatTd"
            min-width="150"
          ></el-table-column>
          <!-- <el-table-column prop="remark" label="备注" align="center" :formatter=formatTd show-overflow-tooltip></el-table-column> -->
          <el-table-column label="备注" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.remark"
                :columnName="'remark'"
              ></copyText>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import common from '../../kits/common'

export default {
  name: 'gridAllocation',
  components: {},
  computed: {
    ...mapState(['investmentOptions', 'areasOptions', 'industryOptions']),
  },
  data() {
    return {
      //搜索条件
      filterField: {
        projectName: '', //项目名称
        areaId: '', //区域
        industryId: '', //行业
        investment: '', //投资类型
        userId: '',
      },
      pageSize: 20,
      pageNo: 1,
      areaId: '', //区域
      isShow: false, //列表加载
      tableList: [],
      advanced: false,
      requestUrl: 'api-p/gridlink/expand/gridlist',
      administratorArr: [],
      loading: true,
      projectOptions: [],
      hiddleToolBar: false,
      tableHeight: null,
      checkedCities: ['01', '05', '10'],
      checkAll: true,
      isIndeterminate: false,
      cities: [
        { code: '01', name: '未处理' },
        { code: '05', name: '处理中' },
        { code: '10', name: '已处理' },
      ],
    }
  },
  created() {
    this.getInvestmenttype({})
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    this.getIndustry({})
    this.getAdministrator()
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll, true)
    let that = this
    this.$nextTick(function () {
      let tableHeightCur = this.$refs.tableBox1.$el.clientHeight
      let tableTop = this.$refs.tableBox.getBoundingClientRect().top
      if (tableHeightCur + tableTop > window.innerHeight) {
        this.tableHeight =
          this.$refs.mainCon.getBoundingClientRect().height - 76
      } else {
        document.getElementsByClassName('tableBoxCur').length &&
          (document.getElementsByClassName('tableBoxCur')[0].style.height =
            window.innerHeight - tableTop - 28 + 'px')
      }
      window.onresize = function () {
        if (!that.$refs.tableBox1) {
          return
        }
        let tableHeightCur = that.$refs.tableBox1.$el.clientHeight
        let tableTop = that.$refs.tableBox.getBoundingClientRect().top
        if (tableHeightCur + tableTop > window.innerHeight) {
          that.tableHeight =
            that.$refs.mainCon.getBoundingClientRect().height - 86
        } else {
          document.getElementsByClassName('tableBoxCur').length &&
            (document.getElementsByClassName('tableBoxCur')[0].style.height =
              window.innerHeight - tableTop - 40 + 'px')
        }
      }
    })
  },
  activated() {
    window.addEventListener('scroll', this.handleScroll, true)
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      this.$refs.tableBox1.doLayout()
    })
  },
  deactivated() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  beforeDestroy() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  watch: {
    areaId(val) {
      console.log(val)
      if (!val.length) {
        this.filterField.areaId = ''
      }
    },
  },
  methods: {
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    ...mapActions(['getInvestmenttype', 'getAreas', 'getIndustry']),
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    bnatchDistribution() {
      this.$router.push({
        path: '/home/networkGrid/bnatchDistribution',
        name: 'bnatchDistribution',
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/networkGrid/networkGridTab',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    getAdministrator() {
      this.axios({
        method: 'post',
        url: 'api-p/grid/expand/findgridusers',
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.administratorArr = response.data.result
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    // 获取项目列表
    getProList(query) {
      this.axios({
        method: 'post',
        data: { projectName: query },
        url: '/api-p/gridlink/expand/gridProjectName',
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.projectList = response.data.result
              if (query !== '') {
                this.loading = true
                setTimeout(() => {
                  this.loading = false
                  this.projectOptions = this.projectList.filter((item) => {
                    return item.toLowerCase().indexOf(query.toLowerCase()) > -1
                  })
                }, 200)
              } else {
                this.projectOptions = []
              }
            } else {
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    remoteMethod(query) {
      this.getProList(query)
    },
    clearOption() {
      this.projectOptions = []
    },
    handleScroll(e) {
      this.$nextTick(function () {
        if (
          this.$refs.tableBox &&
          this.$refs.tableBox.getBoundingClientRect().top < 175
        ) {
          // this.tableHeight = this.$refs.mainCon.getBoundingClientRect().height - 84
          this.tableHeight = window.innerHeight - 218

          if (document.getElementsByClassName('tableBoxCur').length) {
            document.getElementsByClassName('tableBoxCur')[0].style.height =
              null
          }
          this.hiddleToolBar = true
        } else {
          this.hiddleToolBar = false
        }
      })
    },

    //跳转至预警明细页
    linkToNotice(item, index) {
      this.$router.push({
        path: '/home/project/constructAlertProjectList',
        name: 'constructAlertProjectList',
        query: {
          projectName: item.projectName,
          status: index == 1 ? '01' : index == 2 ? '05' : '10',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/project/constructAlertProjectList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    linkToNoticeMonth(item) {
      var year = new Date().getFullYear()
      var month = new Date().getMonth() + 1
      var day = new Date(year, month, 0)

      let starResult = year + '-' + month + '-01'
      let endResult = year + '-' + month + '-' + day.getDate()
      this.$router.push({
        path: '/home/project/constructAlertProjectList',
        name: 'constructAlertProjectList',
        query: {
          projectName: item.projectName,
          status: '10',
          removeTimeStart: starResult,
          removeTimeEnd: endResult,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/project/constructAlertProjectList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    // 导出
    handleCheckAllChange(val) {
      this.checkedCities = val ? ['01', '05', '10'] : []
      this.isIndeterminate = false
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length
      this.checkAll = checkedCount === this.cities.length
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length
    },
    exportExcel() {
      if (this.tableList.length > 0) {
        window.location.href =
          common.commonApi +
          'api-s/report/export_JC_WG_CS_ALERT?projectName=' +
          this.filterField.projectName +
          '&areaId=' +
          this.filterField.areaId +
          '&industryId=' +
          this.filterField.industryId +
          '&investment=' +
          this.filterField.investment +
          '&userId=' +
          this.filterField.userId +
          '&gridAlertStatusJson=' +
          this.checkedCities +
          '&access_token=' +
          localStorage.getItem('access_token')
      } else {
        this.$message({
          type: 'warning',
          message: '无数据导出',
        })
      }
    },
  },
}
</script>
<style lang="less" scoped>
@import '../../../static/css/filterArea.less';
</style>
